package com.pengtu.web.receive.wt01;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.pengtu.dao.Page;
import com.pengtu.dao.QueryFilter;
import com.pengtu.entity.project.Relationship;
import com.pengtu.entity.receive.Entrust;
import com.pengtu.entity.receive.Trialwt01;
import com.pengtu.entity.trial.Order;
import com.pengtu.service.NumberManager;
import com.pengtu.service.project.RelationshipManager;
import com.pengtu.service.receive.TrialCommonManager;
import com.pengtu.service.trial.TrialManager;
import com.pengtu.utils.web.Struts2Utils;
import com.pengtu.web.BaseActionSupport;

@Results({ @Result(name = BaseActionSupport.RELOAD, location = "/receive/wt01/trialwt01.action", type = "redirect")})
public class Trialwt01Action extends BaseActionSupport<Entrust> {

	/**
	 * serialVersionUID:TODO
	 * 
	 * @since 1.0.0
	 */

	private static final long serialVersionUID = 1L;

	private TrialCommonManager trialCommonManager;
	private RelationshipManager relationshipManager;
	private TrialManager trialManager;
	private NumberManager numberManager;

	private String id;
	private Trialwt01 trialwt01;
	private Page<Entrust> page = new Page<Entrust>(10);
	private List<Trialwt01> trialwt01s;
	private List<Trialwt01> oldTrialwt01s;
	private Entrust entrust;
	private Relationship relationship;// 工程
	private Order order;// 检验项目依据标准
	private String[] ord;
	private String relationId;

	@Override
	public Entrust getModel() {
		// TODO Auto-generated method stub
		return entrust;
	}

	@Override
	public String list() throws Exception {
		// TODO Auto-generated method stub
		QueryFilter filters = QueryFilter.buildFromHttpRequest(Struts2Utils
				.getRequest());
		// 设置默认排序方式
		if (!page.isOrderBySetted()) {
			page.setOrderBy("checkdate");
			page.setOrder(Page.DESC);
		}
		filters.addFilter("trialType", "WT01");
		page = trialCommonManager.findByEntrustPage(page, filters);
		return SUCCESS;
	}
	
	public String recheck() throws Exception {
		QueryFilter filters = QueryFilter.buildFromHttpRequest(Struts2Utils
				.getRequest());
		// 设置默认排序方式
		if (!page.isOrderBySetted()) {
			page.setOrderBy("checkdate");
			page.setOrder(Page.DESC);
		}
		filters.addFilter("trialType", "WT01");
		page = trialCommonManager.findByEntrustPage(page, filters);
		return "recheck";
	}

	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		if (StringUtils.isEmpty(id)) {
			entrust.setEntrustNum(numberManager.getRandomNum());
		} else {
			Set<Order> ll = entrust.getOrders();
			relationId = entrust.getRelationship().getId();
			Iterator<Order> ls = ll.iterator();
			if (ls.hasNext()) {
				Order order = ls.next();
				Set<Trialwt01> oldTrialwt01s = order.getTrialwt01s();
				Struts2Utils.getRequest().setAttribute("oldTrialwt01s",
						oldTrialwt01s);
			}
		}
		return INPUT;
	}
	
	public String view() throws Exception {
		if (StringUtils.isEmpty(id)) {
			entrust.setEntrustNum(numberManager.getRandomNum());
		} else {
			Set<Order> ll = entrust.getOrders();
			relationId = entrust.getRelationship().getId();
			Iterator<Order> ls = ll.iterator();
			if (ls.hasNext()) {
				Order order = ls.next();
				Set<Trialwt01> oldTrialwt01s = order.getTrialwt01s();
				Struts2Utils.getRequest().setAttribute("oldTrialwt01s",
						oldTrialwt01s);
			}
		}
		return VIEW;
	}
	public void prepareView()throws Exception{
		prepareModel();
	}

	@Override
	public String save() throws Exception {
		if (StringUtils.isNotEmpty(id)) {
			trialCommonManager.deleteOrderTrialwt01sByEntrust(entrust);
		}
		// TODO Auto-generated method stub
		if (StringUtils.isNotEmpty(relationId)) {
			// 查找对应项目单位信息
			relationship = relationshipManager.findByPropertyName("id",
					relationId);
			// 随机生成受理编号
			entrust.setTrialType("WT01");
			// 将委托单页面信息同步到项目单位中
			if (StringUtils.isNotEmpty(entrust.getWphone()))
				relationship.setWphone(entrust.getWphone());
			if (StringUtils.isNotEmpty(entrust.getWitnesses()))
				relationship.setWitnesses(entrust.getWitnesses());
			if (StringUtils.isNotEmpty(entrust.getCheckpeople()))
				relationship.setCheckpeople(entrust.getCheckpeople());
			if (StringUtils.isNotEmpty(entrust.getCheckphone()))
				relationship.setCheckphone(entrust.getCheckphone());
			if (StringUtils.isNotEmpty(entrust.getOperations()))
				relationship.setOperations(entrust.getOperations());
			if (StringUtils.isNotEmpty(entrust.getOphone()))
				relationship.setOphone(entrust.getOphone());

			// 保存项目单位到委托单
			relationshipManager.saveRelationship(relationship);
			entrust.setRelationship(relationship);
			trialCommonManager.saveEntrust(entrust);

			Set<Trialwt01> pp = new HashSet<Trialwt01>();
			if (trialwt01s != null) {
				for (int i = 0; i < trialwt01s.size(); i++) {
					if (trialwt01s.get(i).getName() != null) {
						Trialwt01 trialwt01 = trialwt01s.get(i);
						trialwt01.setCode(numberManager.getRandomNum());
						pp.add(trialwt01);
						trialCommonManager.saveTrialwt01(trialwt01);
					}
				}
			}
			
			if(oldTrialwt01s!=null){
				for (int i = 0; i < oldTrialwt01s.size(); i++) {
					if (oldTrialwt01s.get(i) != null) {
						pp.add(oldTrialwt01s.get(i));
					}
				}
			}
			trialCommonManager.saveTrialwt01All(pp);
			
			for (int j = 0; j < ord.length; j++) {
				Order order = new Order();
				order.setType(ord[j]);
				order.setStandard(entrust.getStandard());
				order.setTrialtype("WT01");
				order.setEntrust(entrust);
				order.setTrialwt01s(pp);
				trialManager.saveOrder(order);
			}
		}
		return RELOAD;
	}
	
	
	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected void prepareModel() throws Exception {
		// TODO Auto-generated method stub

		if (StringUtils.isNotEmpty(id)) {
			entrust = trialCommonManager.FindByProEntrust("id", id);
		} else {
			entrust = new Entrust();
		}
	}

	public Trialwt01 getTrialwt01() {
		return trialwt01;
	}

	public void setTrialwt01(Trialwt01 trialwt01) {
		this.trialwt01 = trialwt01;
	}

	public Page<Entrust> getPage() {
		return page;
	}

	public void setPage(Page<Entrust> page) {
		this.page = page;
	}

	public Order getOrder() {
		return order;
	}

	public void setOrder(Order order) {
		this.order = order;
	}

	public String[] getOrd() {
		return ord;
	}

	public void setOrd(String[] ord) {
		this.ord = ord;
	}

	public void setId(String id) {
		this.id = id;
	}

	public List<Trialwt01> getTrialwt01s() {
		return trialwt01s;
	}

	public void setTrialwt01s(List<Trialwt01> trialwt01s) {
		this.trialwt01s = trialwt01s;
	}

	/**
	 * relationId
	 * 
	 * @return the relationId
	 * @since 1.0.0
	 */

	public String getRelationId() {
		return relationId;
	}

	/**
	 * @param relationId
	 *            the relationId to set
	 */
	public void setRelationId(String relationId) {
		this.relationId = relationId;
	}

	public List<Trialwt01> getOldTrialwt01s() {
		return oldTrialwt01s;
	}

	public void setOldTrialwt01s(List<Trialwt01> oldTrialwt01s) {
		this.oldTrialwt01s = oldTrialwt01s;
	}

	@Autowired
	public void setTrialCommonManager(TrialCommonManager trialCommonManager) {
		this.trialCommonManager = trialCommonManager;
	}

	@Autowired
	public void setRelationshipManager(RelationshipManager relationshipManager) {
		this.relationshipManager = relationshipManager;
	}

	@Autowired
	public void setTrialManager(TrialManager trialManager) {
		this.trialManager = trialManager;
	}

	@Autowired
	public void setNumberManager(NumberManager numberManager) {
		this.numberManager = numberManager;
	}

}
